import { LANG,TAGS_VIEW } from '@/constant'
import { getItem, setItem } from '@/utils/storage'
export default {
  namespaced: true,
  state: () => ({
    sidebarOpened: true,
    language:getItem(LANG)||'zh',
    tagsViewList:getItem(TAGS_VIEW)||[]
  }),
  mutations: {
    triggerSidebarOpened(state) {
      state.sidebarOpened = !state.sidebarOpened;
    },
    setLanguage(state,lang){
      setItem(LANG,lang)
      state.language=lang
    },
    addTagsViewList(state,tag){
      const ifFind=state.tagsViewList.find(item=>item.path===tag.path)
      if(!ifFind){
        state.tagsViewList.push(tag);
        setItem(TAGS_VIEW,state.tagsViewList)
      }
    },
    changeTagsView(state,{index,tag}){
      state.tagsViewList[index]=tag
      setItem(TAGS_VIEW,state.tagsViewList)
    },
    /**
     * 删除 tag 'other'删除其他 'right'删除右侧 'index'删除当前
     * @param {type: 'other'||'right'||'index', index: index} payload
     */
    removeTagsView(state, payload) {
      if (payload.type === 'index') {
        state.tagsViewList.splice(payload.index, 1)
      } else if (payload.type === 'other') {
        state.tagsViewList.splice(
          payload.index + 1,
          state.tagsViewList.length - payload.index + 1
        )
        state.tagsViewList.splice(0, payload.index)
      } else if (payload.type === 'right') {
        state.tagsViewList.splice(
          payload.index + 1,
          state.tagsViewList.length - payload.index + 1
        )
      }
      setItem(TAGS_VIEW, state.tagsViewList)
    },
  },
  actions: {}
};
